import { createRouter, createWebHistory } from 'vue-router'
import Layout from '@/layout/index.vue'

const routes = [
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/login/index.vue'),
    meta: { title: '登录', hideInMenu: true }
  },
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [
      {
        path: '/dashboard',
        name: 'Dashboard',
        component: () => import('@/views/dashboard/index.vue'),
        meta: { 
          title: '数据概览',
          icon: 'DataAnalysis',
          affix: true
        }
      }
    ]
  },
  {
    path: '/users',
    component: Layout,
    children: [
      {
        path: '/users/list',
        name: 'UserList',
        component: () => import('@/views/users/index.vue'),
        meta: { 
          title: '用户管理',
          icon: 'User'
        }
      }
    ]
  },
  {
    path: '/tables',
    component: Layout,
    children: [
      {
        path: '/tables/list',
        name: 'TableList',
        component: () => import('@/views/tables/index.vue'),
        meta: { 
          title: '台桌管理',
          icon: 'Grid'
        }
      }
    ]
  },
  {
    path: '/orders',
    component: Layout,
    children: [
      {
        path: '/orders/list',
        name: 'OrderList',
        component: () => import('@/views/orders/index.vue'),
        meta: { 
          title: '订单管理',
          icon: 'List'
        }
      }
    ]
  },
  {
    path: '/recharge',
    component: Layout,
    children: [
      {
        path: '/recharge/list',
        name: 'RechargeList',
        component: () => import('@/views/recharge/index.vue'),
        meta: { 
          title: '充值记录',
          icon: 'Money'
        }
      }
    ]
  },
  {
    path: '/settings',
    component: Layout,
    children: [
      {
        path: '/settings/admin',
        name: 'AdminSettings',
        component: () => import('@/views/settings/admin.vue'),
        meta: { 
          title: '管理员设置',
          icon: 'Setting'
        }
      }
    ]
  },
  {
    path: '/staff',
    component: Layout,
    children: [
      {
        path: '/staff/list',
        name: 'StaffList',
        component: () => import('@/views/staff/index.vue'),
        meta: { 
          title: '工作人员管理',
          icon: 'UserFilled'
        }
      }
    ]
  },
  {
    path: '/tasks',
    component: Layout,
    children: [
      {
        path: '/tasks/list',
        name: 'TaskList',
        component: () => import('@/views/tasks/index.vue'),
        meta: { 
          title: '任务管理',
          icon: 'Flag'
        }
      }
    ]
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  const token = localStorage.getItem('admin_token')
  
  if (to.path === '/login') {
    if (token) {
      next('/')
    } else {
      next()
    }
  } else {
    if (token) {
      next()
    } else {
      next('/login')
    }
  }
})

export default router